package com.lanou.dllo.shise.com.lanou.dllo.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.lanou.dllo.shise.R;
import com.lanou.dllo.shise.com.lanou.dllo.activity.OnMyItemClickListener;
import com.lanou.dllo.shise.com.lanou.dllo.entity.SearchBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by dllo on 15/10/30.
 */
public class SearchListAdapter extends RecyclerView.Adapter<SearchListAdapter.ViewHolder> {

    //声明一个这个接口的变量
    private OnMyItemClickListener onMyItemClickListener;
    private RequestQueue queue;
    private List<SearchBean.ResultEntity.TopUserByCityEntity> list;

    public void setOnMyItemClickListener(OnMyItemClickListener ol) {
        this.onMyItemClickListener = ol;
    }

    public SearchListAdapter(Context context){
        queue = Volley.newRequestQueue(context);

    }
    public void addData(List<SearchBean.ResultEntity.TopUserByCityEntity> datas){
        list = datas;
        notifyDataSetChanged();
    }
    @Override
    public SearchListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.recycler_view_search,null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(SearchListAdapter.ViewHolder holder, int position) {
        if (list != null && list.size() > 0) {
            SearchBean.ResultEntity.TopUserByCityEntity topUserByCityEntity = list.get(position);
            holder.search_city_name.setText(topUserByCityEntity.getName());
            holder.search_city_email.setText(topUserByCityEntity.getEmail());
            holder.search_city_level_title.setText(topUserByCityEntity.getLevel_title());
            holder.search_city_last_feedread_time.setText(topUserByCityEntity.getLast_feedread_time());
            if (topUserByCityEntity.getUser_image() != null && topUserByCityEntity.getUser_image().length() > 0){
                String url = topUserByCityEntity.getUser_image();
                getBitmap(url,holder.search_city_user_image);
                holder.itemposition = position;
            }
        }

    }

    private void getBitmap(String url, final ImageView search_city_user_image) {
        ImageRequest pictureRequest = new ImageRequest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                search_city_user_image.setImageBitmap(response);
            }
        }, 720, 1280, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");//添加方法头  避免出现缓存问题
                return super.getHeaders();
            }
        };
        queue.add(pictureRequest);


    }

    @Override
    public int getItemCount() {
        if (list != null && list.size() > 0){
            return list.size();
        }
        return 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private int itemposition;
        private LinearLayout linearLayout;
        private ImageView search_city_user_image;
        private TextView search_city_name, search_city_level_title, search_city_last_feedread_time,
                search_city_email;

        public ViewHolder(View v) {
            super(v);
            linearLayout = (LinearLayout) v.findViewById(R.id.linearLayout);
            linearLayout.setOnClickListener(this);
            search_city_user_image = (ImageView) v.findViewById(R.id.search_city_user_image);
            search_city_name = (TextView) v.findViewById(R.id.search_city_name);
            search_city_email = (TextView) v.findViewById(R.id.search_city_email);
            search_city_level_title = (TextView) v.findViewById(R.id.search_city_level_title);
            search_city_last_feedread_time = (TextView) v.findViewById(R.id.search_city_last_feedread_time);

        }

        @Override
        public void onClick(View v) {
            if (list != null){
                SearchBean.ResultEntity.TopUserByCityEntity li = list.get(itemposition);
                if (onMyItemClickListener != null){
                    onMyItemClickListener.onMyItemClick(li);
                }
            }

        }
    }

}
